<?php
require 'common.php';
require 'mysql.php';
 
// insert into user_qc(uid, qc, calcu) select uid,0,0 from user 


//db //$dbhost, $dbport, $dbuser, $dbpw, $dbname

$game_db = new DBPDO($db_config);

  
for ($i=0;$i<10;$i++){  
	$table_name = 'user_qc_log_'.$i; 
	try{
		$game_db->beginTransaction();//开启事务处理
		$sql = 'UPDATE '.$table_name.' set state = state + 20 where state < 20';
		$rs_update = $game_db->update($sql); 
		echo $rs_update."\n";
		if($rs_update === false){
			throw new PDOException();
		}
// insert into user_qc  select 0,uid,0,0 from user
		$sql = 'update user_qc a inner join 
		(select uid,sum(qc) as qc from '.$table_name.' where state > 20 and state < 50 group by uid) c 
		on a.uid =c.uid set a.qc = a.qc+c.qc';
		$rs_update = $game_db->update($sql); 
		echo $sql."\n";
		echo $rs_update."\n";
		if($rs_update === false){
			throw new PDOException();
		}
		$sql = 'UPDATE '.$table_name.' set state = state + 20 where state > 20 and state < 50';
		$rs_update = $game_db->update($sql); 
		echo $rs_update."\n";
		if($rs_update === false){
			throw new PDOException();
		}
		$game_db->commit();//交易成功就提交
	}catch(PDOException $e){ 
		$game_db->rollback();
	} 
}  
 








